package com.dianrui.moonfire.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dianrui.moonfire.Constant;
import com.dianrui.moonfire.R;
import com.dianrui.moonfire.adapter.ViolationListAdapter;
import com.dianrui.moonfire.bean.ViolationModel;
import com.dianrui.moonfire.okhttp.XHttpRequest;
import com.dianrui.moonfire.util.StringUtils;
import com.dianrui.moonfire.util.ToastUtil;
import com.dianrui.moonfire.util.Url;
import com.google.gson.JsonObject;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
/**
 * Description:违章记录
 * User: luoziyin
 * Date: 2020-8-17
 */
public class ViolationListActivity extends BaseActivity {
    private List<ViolationModel> list = new ArrayList<>();
    private int pageIndex = 1;
    private ViolationListAdapter violationListAdapter;

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;


    @Override
    public int getContentViewId() {
        return R.layout.activity_violationlist;
    }

    @Override
    public void initView() {
        customInit(true);
        title.setText(getString(R.string.no_violation_list_title));
        LinearLayoutManager layoutManager = new LinearLayoutManager(ViolationListActivity.this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerview.setLayoutManager(layoutManager);
        violationListAdapter = new ViolationListAdapter(ViolationListActivity.this,R.layout.item_violation_list, list);
        recyclerview.setAdapter(violationListAdapter);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                getViolation(true);
                refreshLayout.finishRefresh();
            }
        });

        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                pageIndex++;
                getViolation(false);
                refreshLayout.finishLoadMore();
            }
        });
        violationListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ViolationModel violationModel = (ViolationModel)adapter.getItem(position);
                if (violationModel != null) {
                    Intent intent=new Intent(ViolationListActivity.this, ViolationDetailsActivity.class);
                    Bundle bundle=new Bundle();
                    bundle.putString("violation_id", violationModel.violation_id);
                    intent.putExtras(bundle);
                    startActivity(intent);
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        getViolation(false);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (list != null) {
            list.clear();
            list=null;
        }

    }

    @Override
    public void initData() {}



    private void getViolation(final boolean isRfresh) {
        if (isRfresh) {
            pageIndex = 1;
        }
        JsonObject violationList = new JsonObject();
        if (!StringUtils.isEmpty(SPUtils.getInstance().getString("member_id"))) {
            violationList.addProperty("member_id", SPUtils.getInstance().getString("member_id"));
        }
        violationList.addProperty("p", pageIndex);
        XHttpRequest.getInstance().postRequests(Url.VIOLATIONLIST, violationList.toString(), new XHttpRequest.XCallBack() {
            @Override
            public void onResponse(String result) {
                if (isRfresh) {
                    list.clear();
                }
                if (!StringUtils.isEmpty(result)) {
                    try {
                        final JSONObject jsonObject = new JSONObject(result);
                        if (null != jsonObject &&!isFinishing()) {
                            if (Constant.DATASUCCESS.equals(jsonObject.optString("status"))) {
                                JSONArray jsonArray = jsonObject.optJSONArray("data");
                                if (jsonArray.length() > 0) {
                                    for (int i = 0; i < jsonArray.length(); i++) {
                                        ViolationModel violationModel = new ViolationModel();
                                        violationModel.violation_id = jsonArray.optJSONObject(i).optString("violation_id");
                                        violationModel.order_code = jsonArray.optJSONObject(i).optString("order_code");
                                        violationModel.vehicle_number = jsonArray.optJSONObject(i).optString("vehicle_number");
                                        violationModel.title = jsonArray.optJSONObject(i).optString("title");
                                        violationModel.create_time = jsonArray.optJSONObject(i).optString("create_time");
                                        violationModel.state = jsonArray.optJSONObject(i).optString("state");
                                        violationModel.status_name = jsonArray.optJSONObject(i).optString("status_name");
                                        list.add(violationModel);
                                    }
                                    violationListAdapter.setNewData(list);
                                    refreshLayout.setNoMoreData(false);
                                }
                            } else {
                                violationListAdapter.getData().clear();
                                violationListAdapter.setEmptyView(LayoutInflater.from(ViolationListActivity.this).inflate(R.layout.empty_view,null));
                                violationListAdapter.notifyDataSetChanged();
                                ToastUtil.showToast(getString(R.string.no_violation_list_txt));
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                if (list.size() <= 0) {
                    violationListAdapter.getData().clear();
                    violationListAdapter.setEmptyView(LayoutInflater.from(ViolationListActivity.this).inflate(R.layout.empty_view,null));
                    violationListAdapter.notifyDataSetChanged();
                } else {
                    recyclerview.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void failedRequest(String result) {
                ToastUtil.showToast(getString(R.string.net_error));
            }
        });
    }

    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }


}
